# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule


class Tt1Spider(CrawlSpider):
    name = 'tt1'
    allowed_domains = ['tencent.com']
    start_urls = ['http://tencent.com/']

    rules = (
        #自动翻页
        Rule(LinkExtractor(allow=r'position.php?&start=\d+#a'), callback='parse_item', follow=True),
    )

    def parse_item(self, response):
        tr_list = response.xpath('//table[@class="tablelist"]/tr')[1:-1]
        for tr in tr_list:
            item = {}
            item["title"] = tr.xapath('./td[1]/a/text()"]/text()').extract_first()
            item["href"] = "https://hr.tencent.com/" + tr.xapath('./td[1]/a/text()"]/@href').extract_first()
            yield scrapy.Request(
                item["href"],
                callback=self.parse_deatil,
                meta={"item":item},
            )

    # 进入详情页爬取数据
    def parse_detail(self,response):
        item = response.meta["item"]
        item["acquire"] = response.xpath('//div[text()="工作要求："]/../ul/li/text()').extract()
        print(item)


"""
注意1：如果真的有数据在当前页（不在详情页）
"""
